package com.color.admin.common.core.result;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;

/**
 * @author HaiQiu
 * @date 2021/11/18
 * 数据统一返回工具
 */
public class ResultData {

    private static final Boolean TRUE = true;
    private static final Boolean FALSE = false;

    /**
     * 状态码
     */
    private Integer code;
    /**
     * 消息
     */
    private String msg;
    /**
     * 数据
     */
    private Object data;

    /**
     * 响应时间
     */
    @JsonFormat(pattern  = "yyyy-MM-dd HH:mm:ss")
    public Date time;

    /**
     * 是否成功
     */
    private Boolean success;


    public ResultData() {

    }

    public ResultData(Integer code, String msg,Boolean success) {
        this.code = code;
        this.msg = msg;
        this.success = success;
        resultDate();
    }

    public ResultData(Integer code, String msg, Object data,Boolean success) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.success = success;
        resultDate();
    }

    public ResultData(String msg,Boolean success) {
        this.msg = msg;
        this.success = success;
        resultDate();
    }

    public ResultData(Object data,Boolean success) {
        this.data = data;
        this.success = success;
        resultDate();
    }

    public ResultData(String msg, Object data,Boolean success) {
        this.msg = msg;
        this.data = data;
        this.success = success;
        resultDate();
    }

    public ResultData(int code, String msg,Boolean success) {
        this.code = code;
        this.msg = msg;
        this.success = success;
        resultDate();
    }

    public ResultData(int code, Object data,Boolean success) {
        this.code = code;
        this.data = data;
        this.success = success;
        this.time = new Date();
    }

    public static ResultData ok() {
        return new ResultData(ResultCode.OK,TRUE);
    }

    public static ResultData ok(String msg) {
        return new ResultData(ResultCode.OK.code, msg,TRUE);
    }

    public static ResultData ok(Object data) {
        return new ResultData(ResultCode.OK.code, data,TRUE);
    }

    public static ResultData ok(String msg, Object data) {
        return new ResultData(ResultCode.OK.code, msg, data,TRUE);
    }

    public static ResultData ok(Integer code, String msg) {
        return new ResultData(code, msg,TRUE);
    }

    public static ResultData ok(Integer code, String msg, Object data) {
        return new ResultData(code, msg, data,TRUE);
    }

    public static ResultData ok(ResultCode resultCode) {
        return new ResultData(resultCode,TRUE);
    }

    public static ResultData fail() {
        return new ResultData(ResultCode.FAIL.getCode(),ResultCode.FAIL.getMsg(),FALSE);
    }

    public static ResultData fail(String msg) {
        return new ResultData(ResultCode.FAIL.code, msg,FALSE);
    }

    public static ResultData fail(Integer code, String msg) {
        return new ResultData(code, msg,FALSE);
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    private void resultDate(){
        this.time = new Date();
    }

}
